﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ovvio.Web.Core;
using Ovvio.Data;
using System.ComponentModel.DataAnnotations;

namespace Ovvio.Web.Areas.Member.Models
{
    public class MemberModel : BaseModel<User>
    {
        [ScaffoldColumn(false)]
        public int Id { get { return Entity.Id; } set { Entity.Id = value; } }

        public string Name { get { return Entity.LastName + ", " + Entity.FirstName; } }

        public int FirstLevelDownlineCount { get { return Entity.Downline == null ? 0 : Entity.Downline.Count; } }

        public int SecondLevelDownlineCount
        {
            get
            {
                var count = 0;
                if (Entity.Downline == null)
                    return count;

                foreach (var d in Entity.Downline)
                {
                    if (d.Downline == null)
                        continue;

                    if (d.Id == this.Id)
                        continue;

                    count += d.Downline.Count;

                }
                return count;
            }
        }

        public IList<MemberModel> SubMembers
        {
            get 
            {
                var list = new List<MemberModel>();
                if (Entity.Downline.Any())
                {
                    foreach (var d in Entity.Downline)
                        list.Add(new MemberModel() { Entity = d });
                }
                return list;    
            }
        }

        public string Code { get { return Entity.ControlNo; } }
        public DateTime MembershipDate { get { return Entity.RegistrationDate; } }
        public string Upline
        {
            get
            {
                if (Entity.Upline == null)
                    return string.Empty;

                return Entity.Upline.LastName + ", " + Entity.FirstName;
            }
        }
    }
}
